import socket

# 创建socket对象  
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定IP地址和端口号  
server_socket.bind(('127.0.0.1', 8888))

# 开始监听连接请求  
server_socket.listen(1)
print('Server is listening...')

while True:
    # 接受客户端连接请求，并返回连接对象和客户端地址  
    conn, addr = server_socket.accept()
    print('Connected by', addr)

    # 接收客户端发送的数据  
    data = conn.recv(1024)
    print('Received data:', data.decode())

    # 向客户端发送响应数据  
    conn.send(b'Thank you for connecting!')

    # 关闭连接  
    conn.close()